/**
 * 挂号菜单 挂号单历史记录
 */
<template>
  <div>
     <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/operationsummary/Operations' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>挂号历史记录</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
       <el-form-item label="所属科室:">
        <!-- 下拉框 -->
        <el-select size="small" v-model="formInline.officeId" placeholder="所属科室" @change="selectOffice">
          <el-option  label="所有科室" value=""></el-option>          
          <el-option v-for="office in officeList" :label="office.name" :value="office.name" :key="office.id"></el-option>
        </el-select>
      </el-form-item>

        
      <el-form-item label="所属医生:">
        <el-select size="small" v-model="formInline.doctor" placeholder="挂号医生" @change="selectDoctor">
          <el-option  label="全部医生" value=""></el-option>          
          <el-option v-for="doctor in doctorList" :label="doctor.name" :value="doctor.id" :key="doctor.id"></el-option>
        </el-select>
      </el-form-item>
        <!-- 搜索框 -->
      <el-form-item>
          <DateTimeRange @callFather="handleDateTimeRange"></DateTimeRange>
      </el-form-item>
          
      <el-form-item label="患者姓名:">
        <el-input size="small" v-model="formInline.name" placeholder="输入患者姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleCreate()">添加</el-button>
      </el-form-item>

    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column sortable prop="id" label="挂号单号" width="80">
      </el-table-column>
      <el-table-column sortable prop="name" label="患者姓名" width="80">
      </el-table-column>
      <el-table-column sortable prop="patientSex" label="性别" width="80">
      </el-table-column>
      <el-table-column sortable prop="patientAge" label="年龄" width="80">
      </el-table-column>
      <el-table-column sortable prop="patientPhone" label="手机号码" width="100">
      </el-table-column>
      <el-table-column sortable prop="office" label="科室" width="100">
      </el-table-column>
      <el-table-column sortable prop="type" label="接诊类型" width="100">
        
      </el-table-column>
      <el-table-column sortable prop="doctorName" label="接诊医生" width="100">
      </el-table-column>
      <el-table-column sortable prop="date" label="挂号时间" width="100">
      </el-table-column>
      <el-table-column sortable prop="registrationFee" label="挂号费" width="100">        
      </el-table-column>
      <el-table-column sortable prop="treatmentStatus" label="就诊状态" width="100">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button v-if="scope.row.treatmentStatus==='未就诊' && scope.row.chargeStatus===1" size="mini" type="primary" @click="prescriptionCase(scope.row.id,scope.row.patientId,scope.row.name,scope.row.patientAge,scope.row.patientSex,scope.row.office,scope.row.doctorName)">就诊</el-button>
          <el-button v-else :disabled="true" size="mini" type="primary" @click="prescriptionCase(scope.row.id,scope.row.patientId,scope.row.name,scope.row.patientAge,scope.row.patientSex,scope.row.office,scope.row.doctorName)">就诊</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="deletePatient(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->

<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">

      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">

        <el-form-item label="挂号单号" prop="id">
          <el-input size="small" v-model="editForm.id" auto-complete="off" placeholder="挂号单号" disabled="true"></el-input>
        </el-form-item>

        <el-form-item label="科室" prop="office">
          <el-select size="small" v-model="editForm.office" placeholder="科室">                     
            <el-option v-for="role in officeList" :label="role.name" :value="role.name" :key="role.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="医生" prop="doctor">
          <el-select size="small" autocomplete="off" v-model="editForm.doctor" placeholder="医生">                     
            <el-option v-for="doctor in doctorList" :label="doctor.name" :value="doctor.id" :key="doctor.id"></el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="接诊类型" prop="type">
          <el-select size="small" v-model="editForm.type" placeholder="接诊类型">                     
            <el-option label="初诊" value="初诊" ></el-option>
            <el-option label="复诊" value="复诊" ></el-option>
          </el-select>
          
        </el-form-item>
        <el-form-item label="挂号费" prop="registrationFee">
          <el-select size="small" v-model="editForm.registrationFee" placeholder="挂号费" >
          <el-option
            v-for="(label, value) in registerDict.registrationFee"
            :key="value"
            :label="label"
            :value="Number(value)"
          >
          </el-option>
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitUpdateForm('editForm')">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入挂号相关组件
import { registerDict } from "../../dict/registerDict";
import { deptList, deptSave, deptDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'
import axios from 'axios'
// 引入时间段选择组件
import DateTimeRange from '../../components/DateTimeRange'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id:'',
        office:'', 
        type:'',
        doctor:null,
        registrationFee:'',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 10,
        officeId:'',
        doctor:'',
        name: '',
        date:'',
        token: localStorage.getItem('logintoken'),
        dateTimeRange: null,
        registrationFee:'',
      },

      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },

      // 科室列表
      officeList: [] , 
 
      //医生列表
      doctorList: []  ,
      //挂号字典
      registerDict: registerDict,
    }
  },
  // 注册组件
  components: {
    Pagination,
    DateTimeRange
  },
  /**
   * 创建完毕获取员工列表
   * 创建完毕获取诊所列表
   * 创建完毕获取科室列表
   * 创建完毕获取角色列表
   */
  created() {
    this.getdata(this.formInline);
    this.getOfficeList();
    this.getDoctorList();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取员工列表
    getdata(parameter) {
      this.loading = false; 
       let dto = { 

        register: {
        'office':parameter.officeId,
        'doctor':parameter.doctor,
        'date':parameter.date,
        'name':parameter.name,
        'pageNum':parameter.page,
        'pageSize':parameter.limit},

      };   
    // 根据 this.formInline 进行构建 dto
      if (this.formInline.dateTimeRange !== null) {
        dto.register.startDate = this.formInline.dateTimeRange[0];
        dto.register.endDate = this.formInline.dateTimeRange[1];
      }  

      axios.post('/api/register/select',dto)
        .then(res => {
          this.loading = false     
          console.log('结果是',res)     
          this.listData = res.data.data.list
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.data.data.total          
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
     // 时间段选择事件
    handleDateTimeRange(param) {
      this.formInline.dateTimeRange = param
      console.log(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id= row.id
        this.editForm.type= row.type
        this.editForm.date= row.date  
        this.editForm.registrationFee=row.registrationFee
        this.editForm.office= row.office
        this.editForm.doctor=row.doctor
   
      } 
    },
    handleCreate:function(){
        this.$router.push({
        path:"/register/registercreate",
        query:{
          
        }
      })
    },

    // 更新弹窗的保存方法
    submitUpdateForm(editData) {
      // debugger
      this.$refs[editData].validate(valid => {
        if (valid) {
          axios.post('/api/register/update',this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              console.log('更新的结果是',res)
              if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '挂号单更新成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.success('挂号单更新成功！！')
            })
        } else {
          return false
        }
      })
    },
        // 删除挂号
    deletePatient(index, row) {
      // console.log("index", index);
      // console.log("row", row.id);
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送请求 根据id删除 患者
          axios
          ("/api/register/delete/"+row.id)
            .then((res) => {
              this.loading = false;
              // console.log("删除的结果是++++++++++++++++++++++++++++", row.id);
              //  console.log("删除的结果是++++++++++++++++++++++++++++", res);
              this.getdata(this.formInline);
              this.$message.success("删除成功!");
            })
            .catch((err) => {
              // this.loading = false
              // this.$message.error('员工列表加载失败，请稍后再试！')
              this.$message.error("删除失败,请联系管理员!");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },

    // 获取科室列表
    getOfficeList(){
        axios.post('/api/office/selectAllOffice?pageNum=1&pageSize=1000',{})
        .then(res => {
          this.loading = false 
          this.officeList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('科室加载失败，请稍后再试！')
        })
    },
      getDoctorList(){
        axios.post('/api/employee/selectAllEmployee?pageNum=1&pageSize=1000',{roleName:"医生"})
        .then(res => {
          this.loading = false 
          this.doctorList = res.data.data.list                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('科室加载失败，请稍后再试！')
        })
    },
    //获取医生列表
    getDoctorList() {
          console.log("-----------------------------")                  
      axios
        .post('/api/employee/selectAllEmployee?pageNum=1&pageSize=1000',{roleName:"医生"}
        )
         .then(res => {
          this.loading = false 
          this.doctorList = res.data.data.list
          console.log(".......................",res)                  
        })
        .catch(err => {
          this.loading = false
          this.$message.error('科室加载失败，请稍后再试！')
        })
    },
    // 科室选择框改变后调用
    selectOffice(){
      this.getdata(this.formInline)      
    },
    // 医生选择框改变后调用
    selectDoctor(){
      this.getdata(this.formInline)      
    },
    // 跳转到新增处方和病例页
    prescriptionCase(rId,pId,pName,pAge,pSex,office,doctorName){
      this.$router.push({
        path:"/workbench/prescriptionCase",
        query:{
          patientId: pId,
          registerId: rId,
          patientName:pName,
          patientAge:pAge,
          patientSex:pSex,
          office:office,
          doctorName:doctorName
        }
      })
        axios.post('/api/register/updateCase',{
          "id":rId,

          "treatmentStatus":"接诊中"
        })
        .then(res => {
              
              if (res.data.code==200) {
                
                this.$message({
                  type: 'success',
                  message: '接诊中！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('接诊失败，请稍后再试！')
            })


    },

    
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 